<template>
  <view>
    <!-- 搜索框 -->
    <view class="search-box">
      <my-search @myClick="gotoSearch"></my-search>
    </view>
    
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in swiperList" :key="item.goods_id">
        <navigator class="swiper-item" :url="'/subpkg/goods_details/goods_details?goods_id=' + item.goods_id">
          <image :src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类 -->
    <view class="navBox">
      <view class="navItem" v-for="(item, index) in navList" :key="index" @click="navClickHandle(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>

    <!-- 楼层 -->
    <view class="floorBox">
      <view class="floor-item" v-for="(item, index) in floorList" :key="index">
        <image :src="item.floor_title.image_src" class="title-img"></image>
        <view class="item-images">
          <!-- 左侧大图片 -->
          <navigator class="item-images-left-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}"
              mode="widthFix" class="item-images-left"></image>
          </navigator>
          <!-- 右侧四个小图片 -->
          <view class="item-images-right-box">
            <navigator class="item-images-right" :url="rightItem.url"
              v-for="(rightItem, rightIndex) in item.product_list" :key="rightIndex" v-if="rightIndex !== 0">
              <image :src="rightItem.image_src" :style="{width: rightItem.image_width + 'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMixins from '@/mixins/tabbar-badge.js'
  
  export default {
    // 挂载mixins
    mixins: [badgeMixins],
    data() {
      return {
        // 轮播图数据
        swiperList: [],
        // 导航数据
        navList: [],
        // 楼层数据
        floorList: [],
        // 分包的Root路径
        subPkgRoot: '/subpkg/'
      };
    },
    created() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) {
          return uni.showToast({
            title: "请求数据失败",
            duration: 1500,
            icon: "error"
          })
        }
        this.swiperList = res.message
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        // console.log(res)
        this.navList = res.message
      },
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        // console.log(res)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.floorList = res.message
        // 处理图片连接地址中 api和本地不一致的问题
        this.floorList.forEach(item => {
          item.product_list.forEach(product => {
            // 修改api中的数据 product.navigator_url 并存入一个新节点url
            product.url = this.subPkgRoot + 'goods_list/goods_list?' + product.navigator_url.split('?')[1]
            // console.log(product.url)
          })
        })
      },
      navClickHandle(item) {
        // console.log(item)
        if (item.name === '分类') {
          // 跳转到cate的tabbar
          uni.switchTab({
            url: '/pages/cate/cate'
          })
        }
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .search-box {
    // 相当于固定定位, 在vue中使用sticky, 在pc中使用fixed
    position: sticky;
    top: 0;
    z-index: 999;
  }
  
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .navBox {
    display: flex;
    justify-content: space-around;
    height: 150rpx;
    margin: 15px 0;

    .navItem {
      width: 128rpx;
      height: 140rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }
  }

  .floorBox {
    .floor-item {
      margin: 7px;

      .title-img {
        width: 100%;
        height: 60rpx;
      }

      .item-images {
        display: flex;

        .item-images-right-box {
          display: flex;
          flex-wrap: wrap;
          justify-content: space-around;
          margin-left: 3px;
        }
      }

    }
  }
</style>
